#ifndef COOKIE_H
#define COOKIE_H 1

#include "main.h"
#include "array.h"
#include "dict.h"
#include "hashmap.h"

#define COOKIE_HMAP_SIZE 8
#define COOKIE_MAX_LEN 256
#define COOKIE_MAX_KEYLEN 16
#define COOKIE_MAX_VALLEN 48
#define COOKIE_OK MAP_OK
#define COOKIE_MIS MAP_MISSING
#define COOKIE_FULL MAP_FULL
#define COOKIE_OMEM MAP_OMEM

/* define an extra type for all cookies with the same key (seperated with ';') */
typedef map_t ck_map_t;
typedef struct cookie_obj {
  char *key;
  char *val;
  struct cookie_obj *next;
} cookie_obj_t;

#ifdef PRINT_MEM
# define cookie_map_new() hashmap_nnew(COOKIE_HMAP_SIZE); DD("new cookies(%lu)", COOKIE_HMAP_SIZE)
# define cookie_map_free(m) cookie_free(m); hashmap_free(m); DD("free cookies(%p)", m)
#else
# define cookie_map_new() hashmap_nnew(COOKIE_HMAP_SIZE)
# define cookie_map_free(m) cookie_free(m); hashmap_free(m)
#endif
#define cookie_map_get(ck, key, arg) hashmap_get(ck, key, (any_t *) arg)
#define cookie_map_put(ck, key, arg) hashmap_put(ck, key, (any_t *) arg)
#define cookie_iterate(m, i, key, val) hashmap_iterate_all(m, i, key, (any_t *) val)
#define cookie_debug(map) print_hashmap_debug("COOKIE", map);

ck_map_t
parse_cookies(dict_t *http_hdr);

void
insert_cookie(ck_map_t m, char *key, char *value);

void
cookie_free(ck_map_t m);

#endif
